/**
 * AddBillMemberDialog.java
 * com.jamin.aabill.custom.dialog
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-9-3 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.custom.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jamin.aabill.R;
import com.jamin.aabill.custom.dialog.AddBillMemberDialog.BillMemberAdapter.ViewHolder;
import com.jamin.aabill.dao.CircleMemberDao;
import com.jamin.aabill.data.CircleMember;
import com.jamin.aabill.utils.Utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * ClassName:AddBillMemberDialog
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-9-3 上午11:37:26 
 */
public class AddBillMemberDialog extends Dialog {

	private static final String TAG = "AddBillMemberDialog============wangjieming";

	private AddBillMemberDialogLisener addBillMemberDialogLisener = null;

	ListView mMemberListView = null;
	BillMemberAdapter mMemberAdapter = null;
	Button mOKBtn = null;
	Context ctx = null;


	ArrayList<String> mSelectItemName = new ArrayList<String>();
	ArrayList<String> mSelectItemId = new ArrayList<String>();

	List<CircleMember> mCircleList = null;
	long circleId = -1;

	public AddBillMemberDialog(Context context , long circleId , AddBillMemberDialogLisener lisener) {
		super(context , R.style.dialog);
		this.circleId = circleId;
		setCancelable(true);
		setCanceledOnTouchOutside(true);
		this.ctx = context;
		this.addBillMemberDialogLisener = lisener;
		setContentView(R.layout.add_circle_bill_member_dialog);
		initView();

	}



	private void initView() {
		//CircleMember 
		CircleMemberDao mDao = new CircleMemberDao();
		mCircleList = mDao.getCircleMemberList(circleId);
		//List
		mMemberListView = (ListView) findViewById(R.id.add_circle_bill_member_listview);
		mMemberAdapter = new BillMemberAdapter(mCircleList, ctx);
		mMemberListView.setAdapter(mMemberAdapter);
		mMemberListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View v, int position ,long arg3) {
				mMemberAdapter.setCheck(v, position);
				for(int i = 0 ; i < mSelectItemId.size() ;i++){
					Utils.log(TAG, "Item Id = " + mSelectItemId.get(i));
				}
			}
		});

		mOKBtn = (Button) findViewById(R.id.add_circle_bill_member_dialog_bottom_ok);
		mOKBtn.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				dismiss();
				addBillMemberDialogLisener.addMemberView(mSelectItemName , mSelectItemId);
			}
		});
	}




	class BillMemberAdapter extends BaseAdapter{

		List<CircleMember> mAdapterMemberList = null;
		LayoutInflater inflater = null;

		public BillMemberAdapter(List<CircleMember> list , Context ctx){
			this.mAdapterMemberList = list;
			inflater = LayoutInflater.from(ctx);
		}

		@Override
		public int getCount() {
			return mAdapterMemberList.size();

		}

		@Override
		public Object getItem(int position) {
			return mAdapterMemberList.get(position);

		}

		@Override
		public long getItemId(int position) {
			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.add_circle_bill_member_dialog_item, null);
				holder.mCheckBox = (ImageView) convertView.findViewById(R.id.add_bill_dialog_member_item_checkbox);
				holder.mNameView = (TextView) convertView.findViewById(R.id.add_bill_dialog_member_item_name);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mNameView.setText(mAdapterMemberList.get(position).getName());
			if(mSelectItemId.contains(String.valueOf(mAdapterMemberList.get(position).get_id()))){
				holder.mCheckBox.setBackgroundResource(R.drawable.item_checkbox_checked);
				holder.isChecked = true;
			}else{
				holder.mCheckBox.setBackgroundResource(R.drawable.item_checkbox_unchecked);
				holder.isChecked = false;
			}
			return convertView;

		}

		class ViewHolder{

			private boolean isChecked = false;
			ImageView mCheckBox = null;
			TextView mNameView = null;

		}


		public void setCheck(View v , int position){
			ViewHolder holder = (ViewHolder) v.getTag();
			holder.isChecked = !holder.isChecked;
			if (holder.isChecked) {
				if (!mSelectItemId.contains(mAdapterMemberList.get(position).get_id())) {
					mSelectItemId.add(String.valueOf(mAdapterMemberList.get(position).get_id()));
					mSelectItemName.add(mAdapterMemberList.get(position).getName());
				}
				holder.mCheckBox.setBackgroundResource(R.drawable.item_checkbox_checked);
			} else {
				if (mSelectItemId.contains(mAdapterMemberList.get(position).get_id())) {
					mSelectItemId.remove(String.valueOf(mAdapterMemberList.get(position).get_id()));
					mSelectItemName.remove(mAdapterMemberList.get(position).getName());
				}
				holder.mCheckBox.setBackgroundResource(R.drawable.item_checkbox_unchecked);
			}
		}
	}





























	public interface AddBillMemberDialogLisener{
		public void addMemberView(ArrayList<String> memberNameList , ArrayList<String> memberIdList);
	}




}

